﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoctorClient.Common
{
    /// <summary>
    /// 客户端配置类
    /// </summary>
    public static class ClientConfig
    {
        static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region 配置参数

        /// <summary>
        /// 科室
        /// </summary>
        public static string Dept = "";

        /// <summary>
        /// 诊室
        /// </summary>
        public static int Room = 0;

        /// <summary>
        /// 医生
        /// </summary>
        public static string Doctor = "";

        /// <summary>
        /// 新病人通知
        /// </summary>
        public static bool Notify = true;

        #endregion

        /// <summary>
        /// 从App.config文件的appSettings节点中读取配置
        /// </summary>
        public static void LoadConfig()
        {
            try
            {
                System.Collections.Specialized.NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
                if (appSettings != null && appSettings.HasKeys())
                {
                    Dept = Convert.ToString(appSettings["Dept"]);//科室
                    Room = Convert.ToInt32(appSettings["Room"]);//诊室
                    Doctor = Convert.ToString(appSettings["Doctor"]);//医生
                    Notify = Convert.ToBoolean(appSettings["Notify"]);//新病人通知
                }
                else
                {
                    log.Info("未能加载配置！");
                }
            }
            catch (Exception ex)
            {
                log.Error("加载配置异常！", ex);
                throw;
            }
        }

        /// <summary>
        /// 保存配置到App.config文件的appSettings节点中
        /// </summary>
        public static void SaveConfig()
        {
            try
            {
                System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                if (!config.HasFile)
                {
                    throw new ArgumentException("程序配置文件缺失！");
                }

                #region 科室

                if (!config.AppSettings.Settings.AllKeys.Contains("Dept") || config.AppSettings.Settings["Dept"] == null)
                {
                    config.AppSettings.Settings.Add("Dept", Dept);
                }
                else
                {
                    config.AppSettings.Settings["Dept"].Value = Dept;
                }

                #endregion

                #region 诊室

                if (!config.AppSettings.Settings.AllKeys.Contains("Room") || config.AppSettings.Settings["Room"] == null)
                {
                    config.AppSettings.Settings.Add("Room", Convert.ToString(Room));
                }
                else
                {
                    config.AppSettings.Settings["Room"].Value = Convert.ToString(Room);
                }

                #endregion

                #region 医生

                if (!config.AppSettings.Settings.AllKeys.Contains("Doctor") || config.AppSettings.Settings["Doctor"] == null)
                {
                    config.AppSettings.Settings.Add("Doctor", Doctor);
                }
                else
                {
                    config.AppSettings.Settings["Doctor"].Value = Doctor;
                }

                #endregion

                #region 新病人通知

                if (!config.AppSettings.Settings.AllKeys.Contains("Notify") || config.AppSettings.Settings["Notify"] == null)
                {
                    config.AppSettings.Settings.Add("Notify", Doctor);
                }
                else
                {
                    config.AppSettings.Settings["Notify"].Value = Notify.ToString();
                }

                #endregion

                //一定要记得保存，写不带参数的config.Save()也可以
                config.Save(System.Configuration.ConfigurationSaveMode.Modified);
                //刷新，否则程序读取的还是之前的值（可能已装入内存）
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                log.Error("保存配置异常！", ex);
                throw;
            }
        }

    }
}
